'''
Function:
    橡子复仇记
author:
    walker
time:
    2020/5/10
'''
import sys
from config import *
import create_level
from read_file import *

'''开始某一关游戏'''
def start_game(create_level,screen,font,list):
    clock = pygame.time.Clock()
    SCORE = 0
    #墙
    wall_sprites = create_level.setup_walls(list[0])
    #橡子
    acorn_sprites = create_level.set_player(ACORN_PATH,list[2][0])
    #终点
    final_sprites = create_level.set_final(ENDING_PATH,list[3][0])
    #穿越
    trans_sprites,trans_sprites_ori = create_level.set_trans(list[4])
    trans_rect_list = []
    for trans_rect in trans_sprites:
        trans_rect_list.append(trans_rect.rect)

    #水火
    water_sprites = create_level.set_water_fire(WATER_PATH,list[5])
    fire_sprites = create_level.set_water_fire(FIRE_PATH,list[6])
    #火的序列
    fire_rect_list = []
    for fire in fire_sprites:
        fire_rect_list.append(fire.rect)

    #判定是否结束游戏的标志
    is_clearance = False

    # 游戏主循环, running变量会跟踪游戏是否结束, exitcode变量会跟踪玩家是否胜利.
    running, exit_code = True,True
    while running:
        key_list = []
        direction = ''
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(-1)
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == ord('a') or event.key == pygame.K_LEFT:
                    for acorn in acorn_sprites:
                        acorn.move([-1,0])
                        acorn.is_move = True
                        direction = 'a'
                        # print(1)
                        key_list.append(1)
                elif event.key == ord('d') or event.key == pygame.K_RIGHT:
                    for acorn in acorn_sprites:
                        acorn.move([1,0])
                        acorn.is_move = True
                        direction = 'd'
                        # print(2)
                        key_list.append(2)
                elif event.key == pygame.K_UP or event.key == ord('w'):
                    for acorn in acorn_sprites:
                        acorn.move([0,-1])
                        acorn.is_move = True
                        direction = 'w'
                        # print(3)
                        key_list.append(3)
                elif event.key == pygame.K_DOWN or event.key == ord('s'):
                    for acorn in acorn_sprites:
                        acorn.move([0,1])
                        acorn.is_move = True
                        direction = 's'
                        # print(4)
                        key_list.append(4)
            if event.type == pygame.KEYUP:
                if event.key == ord('a') or event.key == pygame.K_LEFT or event.key == ord('d') or event.key == pygame.K_UP or event.key == ord('w') or event.key == pygame.K_DOWN or event.key == ord('s'):
                    acorn.is_move = False
                    # print(5)
                    key_list.append(5)

        screen.fill(BLACK)

        #防止图块跑出屏幕
        for acorn in acorn_sprites:
            acorn.update(wall_sprites,list[2][0],direction)
        acorn_sprites.draw(screen)

        #计算water的个数
        for acorn in acorn_sprites:
            water_l = pygame.sprite.spritecollide(acorn,water_sprites,True)
            acorn.water_num += len(water_l)

        #通过1，2扭曲空间到其对应的地方去
        for acorn in acorn_sprites:
            # acorn_sprites.draw(screen)
            # trans_sprites_change = pygame.sprite.Group()

            if pygame.Rect.collidelist(acorn.rect,trans_rect_list)  == 0:
                acorn.trans(1,trans_rect_list)
                # for trans_rect in trans_sprites:
                #     if trans_rect.rect == trans_rect_list[1]:
                #         trans_rect.base_image == acorn.base_image
                #     trans_sprites_change.add(trans_rect)

            elif pygame.Rect.collidelist(acorn.rect,trans_rect_list) == 1:
                acorn.trans(0,trans_rect_list)

            elif pygame.Rect.collidelist(acorn.rect,trans_rect_list) == 2:
                acorn.trans(3,trans_rect_list)

            elif pygame.Rect.collidelist(acorn.rect,trans_rect_list) == 3:
                acorn.trans(2,trans_rect_list)

        # trans_sprites_change.draw(screen)

        #浇灭火
        for acorn in acorn_sprites:
            if pygame.Rect.collidelist(acorn.rect,fire_rect_list) >= 0:
                fire_rect_list.pop(pygame.Rect.collidelist(acorn.rect,fire_rect_list))
                # print(fire_rect_list)
                if acorn.water_num >= 1:
                    acorn.water_num -= 1
                    fire_l = pygame.sprite.spritecollide(acorn,fire_sprites,True)
                else:
                    is_clearance = False
                    running, exit_code = False, False
                    break

        #将water的个数打印在屏幕右边，并且看本局游戏是否成功
        for acorn in acorn_sprites:
            #获取到每次的步骤
            orient_list = acorn.orient_list
            screen.blit(font.render("Water",True,GREEN),(450,0))
            screen.blit(font.render(str(acorn.water_num),True,RED),(450,15))
            final_l = pygame.sprite.spritecollide(acorn,final_sprites,True)
            if len(final_l) > 0:
                running = False
                exit_code = True

        #将各个元素都画在屏幕中
        wall_sprites.draw(screen)
        water_sprites.draw(screen)
        fire_sprites.draw(screen)
        final_sprites.draw(screen)
        trans_sprites.draw(screen)
        pygame.display.flip()
        clock.tick(7)
    print("Your path is:")
    print(orient_list)
    show_end_game_interface(screen,exit_code,is_clearance)
    return is_clearance

'''游戏结束界面'''
def show_end_game_interface(screen,exit_code,is_clearance):
    font_text = pygame.font.Font(None, 20)
    font = pygame.font.Font(None, 16)
    text = font_text.render("Press ENTER to play again or press ESCAPE to quit",True,WHITE)
    text_rect = text.get_rect()

    while True:
        screen.fill(0)
        if exit_code:
            screen.blit(pygame.image.load(WIN_PATH),(0, 0))
        else:
            screen.blit(pygame.image.load(LOSE_PATH),(0, 0))
        screen.blit(text, (50,50))
        pygame.display.flip()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    main(initialize())
                elif event.key == pygame.K_ESCAPE:
                    sys.exit()
                    pygame.quit()

'''初始化'''
def initialize():
	pygame.init()
	icon_image = pygame.image.load(ACORN_PATH)
	pygame.display.set_icon(icon_image)
	screen = pygame.display.set_mode([500,520])
	pygame.display.set_caption('Angry acorn')
	return screen

'''主函数'''
def main(screen):
    pygame.mixer.init()
    pygame.mixer.music.load(MOONLIGHT_PATH)
    pygame.mixer.music.play(-1,0.0)

    for num_level in range(1,NUM_LEVELS+1):
        if num_level == 1:
            read_content = read_file(LEVEL1)
            level = create_level.create_level()
            content_list = read_content.cal_each_roles_pos()
            is_clearance = start_game(level,screen,font_small,content_list)

if __name__ == '__main__':
    main(initialize())
